package com.bupt.service;

import com.bupt.bean.ChannelDirection;
import com.bupt.bean.DeviceChannel;
import com.bupt.config.CommonCode;
import com.bupt.config.DirectionResult;
import com.bupt.config.Result;
import com.bupt.dao.DeviceChannelRepository;
import com.bupt.dao.DirectionRepository;
import com.bupt.vo.ChannelDirectionVo;
import com.bupt.vo.SysConst;
import org.hibernate.query.internal.NativeQueryImpl;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

@Service
@Transactional
public class DirectionService {


    @PersistenceContext
    private EntityManager entityManager;

    @Autowired
    private DirectionRepository directionRepository;

    @Autowired
    private DeviceChannelRepository deviceChannelRepository;

    @Autowired
    private PrimaryKeyService primaryKeyService;

    //通过设备通道ID查询通道的地理位信息
    public List<ChannelDirectionVo> channelDirectionById(Integer deviceChannelId) {
        //    private Integer channelDirectionId;
        //    private int channelIndex;
        //    private int position;
        //    private String startLatitude;
        //    private String longitude;
        String sql = "SELECT\n" +
                "  channel_direction_id,\n" +
                "  channel_index,\n" +
                "  position,\n" +
                "  longitude,\n" +
                "  latitude\n" +
                " FROM channel_direction_t\n" +
                " WHERE device_channel_id ="+deviceChannelId +" order by position";
        Query query = entityManager.createNativeQuery(sql);
        List<ChannelDirectionVo> channelDirectionVoList = query.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();

        return channelDirectionVoList;
    }

    //通道地理位信息添加页面
    public DirectionResult add(Integer deviceChannelId, Float position, String longitude, String latitude) {
        //deviceChannelId, position,longitude,latitude
        ChannelDirection channelDirection = new ChannelDirection();
        DeviceChannel deviceChannel = deviceChannelRepository.findByDeviceChannelId(deviceChannelId);
        if(deviceChannel!=null){
            int channelIndex = deviceChannel.getChannelIndex();
            Integer channel_section_id = primaryKeyService.getPrimaryKey("CHANNEL_SECTION_ID");
            channelDirection.setChannelDirectionId(channel_section_id);
            channelDirection.setDeviceChannelId(deviceChannelId);
            channelDirection.setChannelIndex(channelIndex);
            channelDirection.setPosition(position);
            channelDirection.setLongitude(longitude);
            channelDirection.setLatitude(latitude);
            /*//这两个需要校验
            channelDirection.setStartPosition(startPosition);
            channelDirection.setEndPosition(endPosition);
            //通达最长米数
            Float channelLen = deviceChannel.getChannelLen();
            if(startPosition>endPosition){
                return new DirectionResult(CommonCode.PARAMFAIL,new ChannelDirection());
            }
            if(channelLen<endPosition){
                return new DirectionResult(CommonCode.BEYONGDFAIL,new ChannelDirection());
            }
            if(endPosition-startPosition>channelLen){
                return new DirectionResult(CommonCode.BEYONGDFAIL,new ChannelDirection());
            }
            //起始位置和结束位置 不能重复
            List<ChannelDirection> allByDeviceChannelList = directionRepository.findAllByDeviceChannelId(deviceChannelId);
            if(allByDeviceChannelList.size()>0){
                for (int i = 0; i <allByDeviceChannelList.size() ; i++) {
                    ChannelDirection channelDirection1 = allByDeviceChannelList.get(i);
                    Float startPosition1 = channelDirection1.getStartPosition();
                    Float endPosition1 = channelDirection1.getEndPosition();
                    if((startPosition<=startPosition1&&endPosition<=startPosition1)||(startPosition>=endPosition1&&endPosition>=endPosition1)){

                    }else{
                        return new DirectionResult(CommonCode.EXISTFAIL,new ChannelDirection());
                    }
                }
            }

            channelDirection.setStartLongitude(longitude);
            channelDirection.setStartLatitude(latitude);
            channelDirection.setEndLongitude(endLongitude);
            channelDirection.setEndLatitude(endLatitude);
            channelDirection.setDirection(direction);*/
            directionRepository.save(channelDirection);

            return new DirectionResult(CommonCode.SUCCESS,channelDirection);
        }else{
            return new DirectionResult(CommonCode.NOTEXISTFAIL,new ChannelDirection());
        }
    }

    //删除通道下的某个地理位置信息
    public Result deleteById(Integer channelDirectionId) {
        Result result = new Result();
        ChannelDirection channelDirection= directionRepository.findByChannelDirectionId(channelDirectionId);
        if(channelDirection!=null){
            directionRepository.deleteByChannelDirectionId(channelDirectionId);


            result.setResult("删除成功");
            return result;
        }
        result.setResult("删除失败");
        return result;
    }

    //通过IP修改单个地理位置信息
    public DirectionResult editDirectionById(Integer deviceChannelId,Integer channelDirectionId, ChannelDirection channelDirection) {
        ChannelDirection byChannelDirection= directionRepository.findByChannelDirectionId(channelDirectionId);
        if (byChannelDirection != null) {
            byChannelDirection.setPosition(channelDirection.getPosition());
            byChannelDirection.setLongitude(channelDirection.getLongitude());
            byChannelDirection.setLatitude(channelDirection.getLatitude());


           /* byChannelDirection.setStartPosition(channelDirection.getStartPosition());
            byChannelDirection.setEndPosition(channelDirection.getEndPosition());

            //起始位置和结束位置 不能重复
            List<ChannelDirection> allByDeviceChannelList = directionRepository.findAllByDeviceChannelId(deviceChannelId);
            if(allByDeviceChannelList.size()>0){
                for (int i = 0; i <allByDeviceChannelList.size() ; i++) {
                    ChannelDirection channelDirection1 = allByDeviceChannelList.get(i);
                    if(channelDirection1.getChannelDirectionId().equals(channelDirectionId)){
                        continue;
                    }
                    Float startPosition1 = channelDirection1.getStartPosition();
                    Float endPosition1 = channelDirection1.getEndPosition();
                    if((channelDirection.getStartPosition()<=startPosition1&&channelDirection.getEndPosition()<=startPosition1)||(channelDirection.getStartPosition()>=endPosition1&&channelDirection.getEndPosition()>=endPosition1)){

                    }else{
                        return new DirectionResult(CommonCode.EXISTFAIL,new ChannelDirection());
                    }
                }
            }


            byChannelDirection.setStartLongitude(channelDirection.getStartLongitude());
            byChannelDirection.setStartLatitude(channelDirection.getStartLatitude());
            byChannelDirection.setEndLongitude(channelDirection.getEndLongitude());
            byChannelDirection.setEndLatitude(channelDirection.getEndLatitude());
            byChannelDirection.setDirection(channelDirection.getDirection());*/

        }

        ChannelDirection save = directionRepository.save(byChannelDirection);
        if (save != null) {
            DirectionResult directionResult = new DirectionResult(CommonCode.SUCCESS, byChannelDirection);
            return directionResult;
        }
        return new DirectionResult(CommonCode.FAIL, null);
    }


    //通道地理位置方向
    public List<SysConst> channelDirection() {
        String sql = "SELECT const_value,const_desc FROM sys_const_t WHERE const_group_name='DIRECTION'";
        Query query = entityManager.createNativeQuery(sql);
        List<SysConst> sysConstList = query.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
        return sysConstList;
    }
}
